package util;

import io.reactivex.MaybeObserver;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.SingleObserver;
import io.reactivex.disposables.Disposable;

public class RxJavaHelper {

    public static Observable<String> getErrorObservable(){
        return Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> observableEmitter) throws Exception {
                observableEmitter.onError(new RuntimeException("getErrorObservable"));
            }
        });
    }

    public static <T> MaybeObserver<T> getMaybeConsumer(){
        return new MaybeObserver<>(){

            @Override
            public void onSubscribe(Disposable disposable) {
                Printer.print("Consumer onSubscribe");
            }

            @Override
            public void onSuccess(T t) {
                Printer.print("Consumer onSuccess t = " + t);
            }

            @Override
            public void onError(Throwable throwable) {
                Printer.print("Consumer onSuccess throwable = " + throwable);
            }

            @Override
            public void onComplete() {
                Printer.print("Consumer onComplete");
            }
        };
    }

    public static <T> SingleObserver<T> getSingleConsumer(){
        return new SingleObserver<T>() {
            @Override
            public void onSubscribe(Disposable disposable) {
                Printer.print("Consumer onSubscribe");
            }

            @Override
            public void onSuccess(T t) {
                Printer.print("Consumer onSuccess = " + t);
            }

            @Override
            public void onError(Throwable throwable) {
                Printer.print("Consumer onError throwable = " + throwable);
            }
        };
    }


    public static <T> Observer<T> getConsumer(){
        return new Observer<>(){

            @Override
            public void onSubscribe(Disposable disposable) {
                Printer.print("Consumer onSubscribe");
            }

            @Override
            public void onNext(T t) {
                Printer.print("Consumer OnNext = " + t);
            }

            @Override
            public void onError(Throwable throwable) {
                Printer.print("Consumer onError throwable = " + throwable);
            }

            @Override
            public void onComplete() {
                Printer.print("Consumer onComplete");
            }
        };
    }

    public static Throwable getException(){
        return new RuntimeException("测试错误");
    }


}
